﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib.MyControls;
using System.Windows.Forms.DataVisualization.Charting;

namespace CarBlackBox
{
    public partial class Form_DriverManage : Form
    {
        public Form_DriverManage()
        {
            InitializeComponent();
        }
        private delegate void renovateDriverInfo();
        public void RenovateDriverInfo()
        {
            this.Invoke(new renovateDriverInfo(delegate() { this.driverInfoTableAdapter.Fill(this.systemDBDataSet.DriverInfo); }));
        }
        private void Form_DriverManage_Load_1(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“systemDBDataSet.DriverInfo”中。您可以根据需要移动或移除它。
            this.driverInfoTableAdapter.Fill(this.systemDBDataSet.DriverInfo);
            toolTip1.SetToolTip(dgv, "双击设置身份卡");
            toolStripComboBox1.Items.Add(new WFNetLib.MyControls.ComboBoxItem("全部", 0));
            toolStripComboBox1.Items.Add(new WFNetLib.MyControls.ComboBoxItem("姓名", 1));
            toolStripComboBox1.Items.Add(new WFNetLib.MyControls.ComboBoxItem("驾驶员编号", 2));
            toolStripComboBox1.Items.Add(new WFNetLib.MyControls.ComboBoxItem("驾驶证号", 3));
            toolStripComboBox1.Items.Add(new WFNetLib.MyControls.ComboBoxItem("主驾车牌号", 4));
            toolStripComboBox1.SelectedIndex = 0;
            toolStripComboBox2.Enabled = false;
        }     

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Form_DriverEditor f = new Form_DriverEditor(this);
            f.ShowDialog();
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            if (dgv.SelectedRows.Count == 0)
                return;
            int index = dgv.SelectedRows[0].Index;
            DriverInfoData d = DriverInfoDataFactory.Construct(dgv, index);
            Form_SetDriverIDCard f = new Form_SetDriverIDCard(d);
            f.ShowDialog();
        }

        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolStripComboBox2.Enabled = true;
            DataTable dt;
            toolStripComboBox2.Items.Clear();
            switch (toolStripComboBox1.SelectedIndex)
            {
                case 0://全部
                    toolStripComboBox2.Enabled = false;
                    break;
                case 1://姓名                    
                    break;
                case 2://驾驶员编号
                    dt = driverInfoTableAdapter.GetDriverIDs();
                    foreach (DataRow dr in dt.Rows)
                    {
                        toolStripComboBox2.Items.Add(dr[DriverInfoData.DRIVERID_Field]);
                    }
                    break;
                case 3://驾驶证号
                    dt = driverInfoTableAdapter.GetDINs();
                    foreach (DataRow dr in dt.Rows)
                    {
                        toolStripComboBox2.Items.Add(dr[DriverInfoData.DIN_Field]);
                    }
                    break;
                case 4://主驾车牌号
                    dt = vehicleInfoTableAdapter.GetLicensePlates();
                    foreach (DataRow dr in dt.Rows)
                    {
                        toolStripComboBox2.Items.Add(dr[VehicleInfoData.LICENSEPLATE_Field]);
                    }
                    break;
            }
        }
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            switch (toolStripComboBox1.SelectedIndex)
            {
                case 0://全部
                    this.driverInfoTableAdapter.Fill(this.systemDBDataSet.DriverInfo);
                    break;
                case 1://姓名
                    if (模糊查询ToolStripMenuItem.Checked)
                    {
                        this.driverInfoTableAdapter.FillByBlurName(this.systemDBDataSet.DriverInfo, toolStripComboBox2.Text);
                    }
                    else
                    {
                        this.driverInfoTableAdapter.FillByName(this.systemDBDataSet.DriverInfo, toolStripComboBox2.Text);
                    }
                    break;
                case 2://驾驶员编号
                    int DriverID;
                    if (int.TryParse(toolStripComboBox2.Text,out DriverID))
                        this.driverInfoTableAdapter.FillByDriverID(this.systemDBDataSet.DriverInfo, DriverID);
                    else
                        MessageBox.Show("驾驶员编号需要为数字!!");
                    break;
                case 3://驾驶证号
                    if (模糊查询ToolStripMenuItem.Checked)
                    {
                        this.driverInfoTableAdapter.FillByBlurDIN(this.systemDBDataSet.DriverInfo, toolStripComboBox2.Text);
                    }
                    else
                    {
                        this.driverInfoTableAdapter.FillByDIN(this.systemDBDataSet.DriverInfo, toolStripComboBox2.Text);
                    }
                    break;
                case 4://主驾车牌号
                    if (模糊查询ToolStripMenuItem.Checked)
                    {
                        this.driverInfoTableAdapter.FillByBlurLicensePlate(this.systemDBDataSet.DriverInfo, toolStripComboBox2.Text);
                    }
                    else
                    {
                        this.driverInfoTableAdapter.FillByLicensePlate(this.systemDBDataSet.DriverInfo, toolStripComboBox2.Text);
                    }
                    break;
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (dgv.SelectedRows.Count == 0)
                return;
            int index = dgv.SelectedRows[0].Index;
            DriverInfoData d = DriverInfoDataFactory.Construct(dgv, index);
            Form_DriverEditor f = new Form_DriverEditor(d);
            if (f.ShowDialog() == DialogResult.OK)
            {
                this.driverInfoTableAdapter.Fill(this.systemDBDataSet.DriverInfo);
                dgv.Rows[index].Selected = true;
            }
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (dgv.SelectedRows.Count == 0)
                return;
            if (WFNetLib.WFGlobal.MessageYseOrNo("确认删除当前的选定项么？", "删除") == DialogResult.No)
                return;
            string DIN = DriverInfoDataFactory.GetPrimaryKey(dgv, dgv.SelectedRows[0].Index);
            //dataGridView1.SelectedRows[0].Cells["车牌号DataGridViewTextBoxColumn"].Value.ToString();
            if (driverInfoTableAdapter.DeleteQuery(DIN)== 1)
            {
                MessageBox.Show("删除成功");
                WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                log.LogType = LogType.驾驶员管理.ToString();
                log.LogContent = "删除驾驶证号为" + DIN + "的驾驶员资料";
                log.LogRemark = "驾驶员删除";
                log.InserSQLLog();
                this.driverInfoTableAdapter.Fill(this.systemDBDataSet.DriverInfo); 
            }
            else
            {
                MessageBox.Show("删除中出现某些问题，请联系管理员");
            }            
        }
    }
}
